<template>
  <div class="airway-clean-container">
    <div class="airway-clean-header">
      <h3>风道清理操作</h3>
    </div>

    <div class="airway-clean-content">
      <div class="status-info">
        <p v-if="isCleaning">正在执行风道清理操作...</p>
        <p v-else>点击下方按钮执行风道清理操作</p>
      </div>

      <div class="airflow-info">
        <h4>当前四路风量</h4>
        <div class="airflow-grid">
          <div class="airflow-item">
            <span class="airflow-label">第一路 (Dode_Vol5):</span>
            <span class="airflow-value">{{
              currentAirflows["2_38_0_0"] || "--"
            }}</span>
          </div>
          <div class="airflow-item">
            <span class="airflow-label">第二路 (Dode_Vol6):</span>
            <span class="airflow-value">{{
              currentAirflows["2_39_0_0"] || "--"
            }}</span>
          </div>
          <div class="airflow-item">
            <span class="airflow-label">第三路 (Dode_Vol8):</span>
            <span class="airflow-value">{{
              currentAirflows["2_40_0_0"] || "--"
            }}</span>
          </div>
          <div class="airflow-item">
            <span class="airflow-label">第四路 (Dode_Vol10):</span>
            <span class="airflow-value">{{
              currentAirflows["2_36_0_0"] || "--"
            }}</span>
          </div>
        </div>
      </div>

      <div class="clean-target-info">
        <h4>清理目标值</h4>
        <p>将四路风量恢复到正常值</p>
      </div>

      <div class="action-buttons">
        <el-button
          class="clean-button"
          type="primary"
          :loading="isCleaning"
          @click="cleanAirway"
          :disabled="isCleaning"
        >
          {{ isCleaning ? "清理中..." : "清理风道" }}
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import { setAlgorithmParam } from "@/api/util";
import { useUserStore } from "@/store/user";
import sendToUnity from "@/core/communication/SendToUnity";
import { SendMessageType } from "@/core/config/DeclareGlobal";

// 获取用户store实例
const userStore = useUserStore();

// 定义props
const props = defineProps({
  initialAirflows: {
    type: Object,
    default: () => ({
      "2_38_0_0": 2.5,
      "2_39_0_0": 2.5,
      "2_40_0_0": 2.5,
      "2_36_0_0": 2.5,
    }),
  },
});

// 定义emit
const emit = defineEmits(["cleaned"]);

// 当前风量参数
const currentAirflows = reactive({
  ...props.initialAirflows,
});

// 清理状态
const isCleaning = ref(false);

// 需要设置的参数列表
const airflowParams = [
  "2_38_0_0", // 第一路的给定风冷流量(Dode_Vol5)
  "2_39_0_0", // 第二路的给定风冷流量(Dode_Vol6)
  "2_40_0_0", // 第三路的给定风冷流量(Dode_Vol8)
  "2_36_0_0", // 第四路的给定风冷流量(Dode_Vol10)
];

// 清理风道函数
const cleanAirway = async () => {
  if (isCleaning.value) return;

  isCleaning.value = true;
  try {
    // 获取用户的copyNum
    const copyNum = userStore.copyNum;

    // 逐个设置参数值为2.5
    const promises = airflowParams.map((param) =>
      setAlgorithmParam(param, 2.5, copyNum)
    );

    // 等待所有设置完成
    await Promise.all(promises);

    // 更新当前状态
    airflowParams.forEach((param) => {
      currentAirflows[param] = 2.5;
    });
    //向unity发送风道清理消息
    sendToUnity.send({
      type: SendMessageType.ClearWindRoadBlock,
      data: {},
    });

    ElMessage.success("风道清理操作已成功完成");

    // 触发清理完成事件
    emit("cleaned", currentAirflows);
  } catch (error) {
    ElMessage.error("风道清理操作失败，请重试");
    console.error("清理风道出错:", error);
  } finally {
    isCleaning.value = false;
  }
};
</script>

<style scoped>
.airway-clean-container {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
  border-radius: 8px;
  color: #f1f5f9;
  display: flex;
  flex-direction: column;
  padding: 20px;
}

.airway-clean-header {
  border-bottom: 1px solid #334155;
  padding-bottom: 15px;
  margin-bottom: 20px;
}

.airway-clean-header h3 {
  margin: 0;
  color: #60a5fa;
  font-size: 18px;
  font-weight: 600;
}

.airway-clean-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.status-info {
  text-align: center;
  padding: 15px;
  background-color: rgba(59, 130, 246, 0.1);
  border-radius: 6px;
  border-left: 4px solid #60a5fa;
}

.status-info p {
  margin: 0;
  font-size: 14px;
}

.airflow-info h4,
.clean-target-info h4 {
  margin: 0 0 10px 0;
  color: #94a3b8;
  font-size: 16px;
  font-weight: 500;
}

.airflow-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 15px;
}

.airflow-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background-color: rgba(255, 255, 255, 0.05);
  border-radius: 6px;
  font-size: 14px;
}

.airflow-label {
  color: #cbd5e1;
}

.airflow-value {
  color: #60a5fa;
  font-weight: 600;
  font-family: "Courier New", monospace;
}

.clean-target-info {
  padding: 15px;
  background-color: rgba(16, 185, 129, 0.1);
  border-radius: 6px;
  border-left: 4px solid #10b981;
}

.clean-target-info p {
  margin: 0;
  font-size: 14px;
}

.target-value {
  color: #10b981;
  font-weight: 600;
  font-size: 16px;
}

.action-buttons {
  display: flex;
  justify-content: center;
  margin-top: auto;
  padding-top: 20px;
}

.clean-button {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  border: none;
  color: white;
  padding: 12px 36px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.clean-button:hover:not(:disabled) {
  background: linear-gradient(135deg, #059669 0%, #047857 100%);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
}

.clean-button:active:not(:disabled) {
  transform: translateY(0);
}

.clean-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .airflow-grid {
    grid-template-columns: 1fr;
  }

  .clean-button {
    width: 100%;
  }
}
</style>